<?php

/******************************************************
 *					  	 COMMON
 ******************************************************/

/**
 * Checks if a string is a well formated email
 * @param $email The string
 * @return Boolean
 * @author Pablo
 * @since 1.0
 */
function v_is_email($email){
	if(!filter_var($email, FILTER_VALIDATE_EMAIL))
		return FALSE;
	
	return TRUE;
}

/**
 * Checks if all of the characters in the provided string are alphanumeric. 
 * @param $str
 * @return Boolean
 * @author Pablo
 * @since 1.0
 */
function v_is_alfanumeric($str){
	return ctype_alnum($str);
}

/**
 * Checks if all of the characters in the provided string are alphabetic. 
 * @param $str
 * @return Boolean
 * @author Pablo
 * @since 1.0
 */
function v_is_alpha($str){
	return ctype_alpha($str);
}

/**
 * Checks if all of the characters in the provided string are numerical. 
 * @param $str
 * @return Boolean
 * @author Pablo
 * @since 1.0
 */
function v_is_numeric($str){
	return ctype_digit($str);	
}

/**
 * Checks if all of the characters in the provided string are 'hexadecimal' digits. 
 * @param $str
 * @return Boolean
 * @author Pablo
 * @since 1.0
 */
function v_is_hexadecimal($str){
	return ctype_xdigit($str);
}

/**
 * Checks if all of the characters in the provided string are uppercase characters.. 
 * @param $str
 * @return Boolean
 * @author Pablo
 * @since 1.0
 */
function v_is_uppercase($str){
	return ctype_upper($str);
}

/**
 * Checks if all of the characters in the provided string lowercase characters.. 
 * @param $str
 * @return Boolean
 * @author Pablo
 * @since 1.0
 */
function v_is_lowercase($str){
	return ctype_lower($str);
}

/******************************************************
 *						USERS					  	 
 ******************************************************/

/**
 * Validates a new user data
 * @param $name
 * @param $username
 * @param $password1
 * @param $password2
 * @param $email
 * @return Boolean
 * @author Pablo
 * @since 1.0
 */
function v_blg_new_user($name, $username, $email, $pass1, $pass2){
	$errors= FALSE;
	
	//Check for errors
	if(blg_email_exists($email)){
		blg_msg_add(blg_echo('error:email:already_exists', array($email)), TRUE);
		$errors= TRUE;
	}
	if(blg_username_exists($username)){
		blg_msg_add(blg_echo('error:username:already_exists', array($username)), TRUE);
		$errors= TRUE;
	}
	
	//Name
	if(strlen($name)==0){
		blg_msg_add(blg_echo('error:name:empty'), TRUE);
		$errors= TRUE;
	}	
	
	//Username
	if(strlen($username)==0){
		blg_msg_add(blg_echo('error:username:empty'), TRUE);
		$errors= TRUE;
	}
	elseif(strlen($username) < blg_get_config('username_min_length')){
		blg_msg_add(blg_echo('error:username:min_length'), TRUE);
		$errors= TRUE;
	}
	elseif(!ctype_alnum($username)){
		blg_msg_add(blg_echo('error:username:not_alphanumeric'), TRUE);
		$errors= TRUE;
	}
	
	//Email
	if(strlen($email)<1 || !v_is_email($email)){
		blg_msg_add(blg_echo('error:email:invalid'), TRUE);
		$errors= TRUE;	
	}
	
	//Passwords
	if(strlen($pass1) < 1 ){
		blg_msg_add(blg_echo('error:password:empty'), TRUE);
		$errors= TRUE;
	}
	elseif(strlen($pass1) < blg_get_config('password_min_length')){
		blg_msg_add(blg_echo('error:password:min_length'), TRUE);
		$errors= TRUE;	
	}
	elseif($pass1!==$pass2){
		blg_msg_add(blg_echo('error:password:not_match'), TRUE);
		$errors= TRUE;
	}
	elseif($pass1==$username){
		blg_msg_add(blg_echo('error:password:equals_username'), TRUE);
		$errors= TRUE;
	}
	
	return ($errors===FALSE);
}

